/*
*  Copyright (c) 2001 Sun Microsystems, Inc.  All rights
*  reserved.
*
*  Redistribution and use in source and binary forms, with or without
*  modification, are permitted provided that the following conditions
*  are met:
*
*  1. Redistributions of source code must retain the above copyright
*  notice, this list of conditions and the following disclaimer.
*
*  2. Redistributions in binary form must reproduce the above copyright
*  notice, this list of conditions and the following disclaimer in
*  the documentation and/or other materials provided with the
*  distribution.
*
*  3. The end-user documentation included with the redistribution,
*  if any, must include the following acknowledgment:
*  "This product includes software developed by the
*  Sun Microsystems, Inc. for Project JXTA."
*  Alternately, this acknowledgment may appear in the software itself,
*  if and wherever such third-party acknowledgments normally appear.
*
*  4. The groups "Sun", "Sun Microsystems, Inc.", "JXTA" and "Project JXTA"
*  must not be used to endorse or promote products derived from this
*  software without prior written permission. For written
*  permission, please contact Project JXTA at http://www.jxta.org.
*
*  5. Products derived from this software may not be called "JXTA",
*  nor may "JXTA" appear in their group, without prior written
*  permission of Sun.
*
*  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
*  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
*  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
*  DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
*  ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
*  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
*  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
*  USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
*  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
*  OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
*  OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
*  SUCH DAMAGE.
*  ====================================================================
*
*  This software consists of voluntary contributions made by many
*  individuals on behalf of Project JXTA.  For more
*  information on Project JXTA, please see
*  <http://www.jxta.org/>.
*
*  This license is based on the BSD license adopted by the Apache Foundation.
*
*  $Id: GroupManagementPanel.java,v 1.2 2006/05/17 21:23:56 nano Exp $
*/

package net.jxta.myjxta.ui;

import net.jxta.myjxta.util.Resources;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;

import javax.swing.AbstractAction;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSlider;

import java.util.ResourceBundle;

/**
 *
 * @version $Id: GroupManagementPanel.java,v 1.2 2006/05/17 21:23:56 nano Exp $
 *
 * @author james todd [gonzo at jxta dot org]
 */

public class GroupManagementPanel
    extends JPanel {

    private static final ResourceBundle STRINGS = Resources.getStrings();
    private static final int MINIMUM_AUTO_RENDEZVOUS_PERIOD = 0;
    private static final int INITIAL_AUTO_RENDEZVOUS_PERIOD =
        net.jxta.myjxta.util.Constants.AUTO_RENDEZVOUS_JOIN_PERIOD / 1000;
    private static final int MAXIMUM_AUTO_RENDEZVOUS_PERIOD = 10 * 60;
    
    private JSlider autoRendezVousPeriod = null;
    private boolean isCancel = false;
    private JButton ok = null;

    public GroupManagementPanel() {
        super();

        ui();
    }

    public int getAutoRendezVousPeriod() {
        return this.autoRendezVousPeriod.getValue() * 1000;
    }

    public boolean isCancel() {
        return this.isCancel;
    }

    private void ui() {
        GridBagLayout gb = new GridBagLayout();
        GridBagConstraints gbc = new GridBagConstraints();

        setLayout(gb);
        setBorder(BorderFactory.createEmptyBorder(3, 3, 3, 3));

        JLabel l = new JLabel(STRINGS.getString("label.group.autoRendezVous.period"));

        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.gridwidth = 1;
        gbc.gridheight = 1;
        gbc.weightx = 1.0;
        gbc.weighty = 1.0;
        gbc.anchor = GridBagConstraints.FIRST_LINE_START;
        gbc.fill = GridBagConstraints.BOTH;
        gbc.insets = new Insets(3, 3, 3, 3);
        gbc.ipadx = 0;
        gbc.ipady = 0;

        gb.setConstraints(l, gbc);

        add(l);
        
        this.autoRendezVousPeriod = new JSlider(JSlider.HORIZONTAL,
            MINIMUM_AUTO_RENDEZVOUS_PERIOD, MAXIMUM_AUTO_RENDEZVOUS_PERIOD,
            INITIAL_AUTO_RENDEZVOUS_PERIOD);
        
        this.autoRendezVousPeriod.setMajorTickSpacing(INITIAL_AUTO_RENDEZVOUS_PERIOD);
        this.autoRendezVousPeriod.setMinorTickSpacing(MINIMUM_AUTO_RENDEZVOUS_PERIOD);
        this.autoRendezVousPeriod.setPaintTicks(true);
        this.autoRendezVousPeriod.setPaintLabels(true);
        
        gbc.gridx++;
        gbc.gridwidth = GridBagConstraints.REMAINDER;
        
        gb.setConstraints(this.autoRendezVousPeriod, gbc);
        
        add(this.autoRendezVousPeriod);
        
        JPanel bp = new JPanel();
        
        bp.setLayout(new GridLayout(1, 0));
        
        this.ok = new JButton(new AbstractAction() {
            public void actionPerformed(ActionEvent ae) {
                if (isValidInput()) {
                    exit();
                }
            }
        });
        
        this.ok.setText(STRINGS.getString("action.ok"));
        this.ok.addKeyListener(new AbstractButtonKeyListener(this.ok) {
            public void keyReleased(KeyEvent ke) {
                getButton().getAction().actionPerformed(null);
            }
        });
        this.ok.setEnabled(isValidInput());
        
        bp.add(this.ok);

        JButton b = new JButton(new AbstractAction() {
            public void actionPerformed(ActionEvent ae) {
                setCancel(true);
                exit();
            }
        });

        b.setText(STRINGS.getString("action.cancel"));
        b.addKeyListener(new AbstractButtonKeyListener(b) {
            public void keyReleased(KeyEvent ke) {
                getButton().getAction().actionPerformed(null);
            }
        });

        bp.add(b);

        gbc.gridx = 0;
        gbc.gridy++;
        gbc.gridwidth = 2;
        gbc.anchor = GridBagConstraints.LAST_LINE_END;
        gbc.fill = GridBagConstraints.NONE;

        gb.setConstraints(bp, gbc);

        add(bp);

//        getRootPane().setDefaultButton(this.ok);
    }

    private void setCancel(boolean isCancel) {
        this.isCancel = isCancel;
    }

    private void exit() {
        getRootPane().getParent().setVisible(false);
    }

    private boolean isValidInput() {
        return true;
    }
}
